#include <cv_bridge/cv_bridge.h>
#include <image_transport/image_transport.h>
#include <nodelet/nodelet.h>
#include <pluginlib/class_list_macros.h>
#include <ros/ros.h>

#include <opencv2/opencv.hpp>

namespace libav_nodelet {

class RecvNodelet : public nodelet::Nodelet {
 public:
  RecvNodelet(){};
  ~RecvNodelet(){};

 private:
  virtual void onInit() {
    ros::NodeHandle nh = getNodeHandle();
    ros::NodeHandle pnh = getPrivateNodeHandle();

    image_transport::ImageTransport it(nh);
    img_sub_ = it.subscribe("/camera/image", 1, &RecvNodelet::imageCallback, this);
  };

  void imageCallback(const sensor_msgs::ImageConstPtr& msg) {
    try {
      cv::imshow("nodelet_view", cv_bridge::toCvShare(msg, "bgr8")->image);
      cv::waitKey(10);
    } catch (cv_bridge::Exception& e) {
      NODELET_ERROR("Could not convert from '%s' to 'bgr8'.",
                    msg->encoding.c_str());
    }
  }

  image_transport::Subscriber img_sub_;
};

}  // namespace test_recv

PLUGINLIB_EXPORT_CLASS(libav_nodelet::RecvNodelet, nodelet::Nodelet);